/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clienttest.common;

import clienttest.SavingAccount;
import clienttest.object.CreditCard;
import clienttest.object.ResultReponse;
import clienttest.object.SavingAccountObject;
import com.google.gson.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author lekhanhduy
 */
public class ConnectManager {

    public static final String HOSTNAME = "localhost";
    public static final int PORT = 2007;
    public JFrame contener = null;

    public ConnectManager() {
    }

    public ConnectManager(JFrame conterner) {
        this.contener = conterner;
    }

    public String sendRevRequest(String request) {
        PrintWriter pw = null;
        BufferedReader br = null;

        try {
            Socket s = new Socket(HOSTNAME, PORT);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw = new PrintWriter(s.getOutputStream());
            System.out.println("Da ket noi duoc voi server...");
            while (true) {
                //gui request len server
                pw.println(request);
                pw.flush();
                //doc het ket qua tu server tra ve
                StringBuilder bulder = new StringBuilder();
                String result = null;
                while ((result = br.readLine()) != null) {
                    bulder.append(result);
                }
                return bulder.toString();
            }
        } catch (IOException e) {
            if (contener != null) {
                JOptionPane.showMessageDialog(contener,
                        "Lỗi kết nối với server!",
                        "Lỗi kết nối!",
                        JOptionPane.ERROR_MESSAGE);
            }
            System.err.println(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (pw != null) {
                    pw.close();
                }
            } catch (IOException e) {
                if (contener != null) {
                    JOptionPane.showMessageDialog(contener,
                            "Lỗi kết nối với server!",
                            "Lỗi kết nối!",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        return null;
    }

    public boolean login(String accountNumber, String pass) {
        String loginMethod = "login/" + accountNumber + "/" + pass;
        return Boolean.valueOf(sendRevRequest(loginMethod));
    }

    public boolean loginSavingAccount(String accountNumber) {
        String loginMethod = "loginSavingAccount/" + accountNumber;
        String result = sendRevRequest(loginMethod);
        return Boolean.valueOf(result);
    }

    public CreditCard getCreditCardOfUser(String accountNumber) {
        CreditCard creditcard = null;
        String method = "getCreditCardOfUser/" + accountNumber;
        String result = sendRevRequest(method);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            creditcard = gson.fromJson(result, CreditCard.class);
        }
        return creditcard;
    }

    public SavingAccountObject getInfoSavingAccount(String accountNumber) {
        SavingAccountObject savingAccount = null;
        String method = "getInfoSavingAccount/" + accountNumber;
        String result = sendRevRequest(method);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            savingAccount = gson.fromJson(result, SavingAccountObject.class);
        }
        return savingAccount;
    }

    public ResultReponse sentWithDrawMoney(String accountName, String method, double money) {
        String nameMethod = "sentWithDrawMoney/" + accountName;
        ResultReponse resultReponse = null;
        if (method.equals("sent")) {
            nameMethod += "/sent/" + money;
        }
        if (method.equals("withdraw")) {
            nameMethod += "/withdraw/" + money;
        }
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }

    public ResultReponse check_transfers(String user) {
        String nameMethod = "check_transfers/" + user;
        ResultReponse resultReponse = null;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }

    public ResultReponse transfersCreditCard(String userSent, String userRev, double money) {
        String nameMethod = "transfers_creditcard/" + userSent + "/" + userRev + "/" + String.valueOf(money);
        ResultReponse resultReponse = null;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }

    public Object[][] getAllHistoryCreditCard(String user, String dateFrom, String dateTo) {
        String nameMethod = "getAllHistoryCreditCard/" + user + "/" + dateFrom + "/" + dateTo;
        String result = sendRevRequest(nameMethod);
        JsonParser parsel = new JsonParser();
        Object obj = parsel.parse(result);
        JsonArray array = (JsonArray) obj;
        Object[][] temp = new Object[array.size()][5];
        Gson gson = new GsonBuilder().serializeNulls().create();
        for (int i = 0; i < array.size(); i++) {
            Object obj2 = (Object) array.get(i);
            JsonArray array2 = (JsonArray) obj2;
            temp[i][0] = gson.fromJson(array2.get(0).toString(), String.class);
            temp[i][1] = gson.fromJson(array2.get(1).toString(), String.class);
            temp[i][2] = gson.fromJson(array2.get(2).toString(), String.class);
            temp[i][3] = gson.fromJson(array2.get(3).toString(), String.class);
            double balance = gson.fromJson(array2.get(4).toString(), Double.class);
            temp[i][4] = (DecimalFormat.getCurrencyInstance(Locale.US).format(balance)).replace("$", " ") + " VND";
        }
        return temp;
    }

    public ResultReponse changePassCreditCard(String user, String pass) {
        String nameMethod = "changePassCreditCard/" + user + "/" + pass;
        ResultReponse resultReponse = null;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }

    public Object[][] getAllUserCreditCard() {
        String nameMethod = "getAllUserCreditCard";
        String result = sendRevRequest(nameMethod);
        JsonParser parsel = new JsonParser();
        Object obj = parsel.parse(result);
        JsonArray array = (JsonArray) obj;
        Object[][] temp = new Object[array.size()][8];
        Gson gson = new GsonBuilder().serializeNulls().create();
        for (int i = 0; i < array.size(); i++) {
            Object obj2 = (Object) array.get(i);
            JsonArray array2 = (JsonArray) obj2;
            temp[i][0] = gson.fromJson(array2.get(0).toString(), String.class);
            temp[i][1] = gson.fromJson(array2.get(1).toString(), String.class);
            temp[i][2] = gson.fromJson(array2.get(2).toString(), String.class);
            double balance = gson.fromJson(array2.get(3).toString(), Double.class);
            temp[i][3] = (DecimalFormat.getCurrencyInstance(Locale.US).format(balance)).replace("$", " ") + " VND";
            temp[i][4] = gson.fromJson(array2.get(4).toString(), String.class);
            temp[i][5] = gson.fromJson(array2.get(5).toString(), String.class);
            temp[i][6] = gson.fromJson(array2.get(6).toString(), String.class);
            temp[i][7] = gson.fromJson("false", Boolean.class);
        }
        return temp;
    }

    public ResultReponse withDrawMoneySavingAccount(String accountNumber, double money) {
        String nameMethod = "withDrawMoneySavingAccount/" + accountNumber + "/" + money;
        ResultReponse resultReponse = null;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }

    public Object[][] getAllHistorySavingAccount(String user, String dateFrom, String dateTo) {
        String nameMethod = "getAllHistorySavingAccount/" + user + "/" + dateFrom + "/" + dateTo;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            JsonParser parsel = new JsonParser();
            Object obj = parsel.parse(result);
            JsonArray array = (JsonArray) obj;
            Object[][] temp = new Object[array.size()][4];
            Gson gson = new GsonBuilder().serializeNulls().create();
            for (int i = 0; i < array.size(); i++) {
                Object obj2 = (Object) array.get(i);
                JsonArray array2 = (JsonArray) obj2;
                temp[i][0] = gson.fromJson(array2.get(0).toString(), String.class);
                temp[i][1] = gson.fromJson(array2.get(1).toString(), String.class);
                temp[i][2] = gson.fromJson(array2.get(2).toString(), String.class);
                double balance = gson.fromJson(array2.get(3).toString(), Double.class);
                temp[i][3] = (DecimalFormat.getCurrencyInstance(Locale.US).format(balance)).replace("$", " ") + " VND";
            }

            return temp;
        }else 
        {
            return null;
        }
    }
    
    public ResultReponse caculInterest(String accountNumber, String date) {
        String nameMethod = "caculInterest/" + accountNumber + "/" + date;
        ResultReponse resultReponse = null;
        String result = sendRevRequest(nameMethod);
        if (result != null) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            resultReponse = gson.fromJson(result, ResultReponse.class);
        }
        return resultReponse;
    }
}
